package com.by.controller;


import com.by.entity.Product;
import com.by.entity.Sku;
import com.by.result.Result;
import com.by.service.ProductService;
import com.by.service.SkuService;
import com.by.vo.ProductDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;
    /**
     * 查询spu商品列表
     * @return spu商品列表
     */
    @GetMapping("/list")
    public Result spuList(){
        List<Product> list = productService.list();
        return Result.success(list);
    }

    /**
     * 查询商品详情
     * @param skuId 商品id
     * @return 商品详情
     */
    @GetMapping("/detail/{skuId}")
    public Result<ProductDetailVo> detail(@PathVariable Long skuId) {
        ProductDetailVo detail = productService.getProductDetail(skuId);
        return Result.success(detail);
    }

    /**
     * 根据分类id查询商品列表spu
     * @param catId 分类id
     * @return 商品列表
     */
     @GetMapping("/list/{catId}")
    public Result<List<Product>> list(@PathVariable Long catId) {
        List<Product> list = productService.listByCategoryId(catId);
        return Result.success(list);
    }
}
